/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "S_DX顶点数组.h"




S_DX缓存::S_DX缓存() {
	/*D3D12_INPUT_ELEMENT_DESC 顶点描述[] = {
		{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
		{"NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
		{"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UINT, 0, 24, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
	};*/

	m_CPU_Buffer	= nullptr;
	m_GPU_Buffer	= nullptr;
	m_数据上传		= nullptr;

}

uint32 S_DX缓存::f_创建内存对象(const void* c, void* ctx, uint32 key) {
	const S_GPU缓存创建参数	创建参数	= *((S_GPU缓存创建参数*)c);
	ID3D12Device4*			dev			= (ID3D12Device4*)ctx;
	

	m_Count		= 创建参数.length;
	m_BitOffset	= 创建参数.byteOffset;
	uint64 byteSize = 创建参数.length*创建参数.byteOffset;



	//CD3DX12_RESOURCE_DESC Buffer = f_DX_1D缓存参数数组(byteSize);
	CD3DX12_RESOURCE_DESC Buffer = CD3DX12_RESOURCE_DESC::Buffer(byteSize);

	D3D12_HEAP_PROPERTIES properties = f_缓存性能设置参数(D3D12_HEAP_TYPE_DEFAULT);
	// Create the actual default buffer resource.
	// 将CPU端内存中的数据复制到默认缓冲区 中介位置上传堆
	HRESULT hr = dev->CreateCommittedResource(
		&properties,
		D3D12_HEAP_FLAG_NONE,
		&Buffer,
		D3D12_RESOURCE_STATE_COMMON,
		nullptr,
		IID_PPV_ARGS(m_GPU_Buffer.GetAddressOf())
	);
	



	D3D12_HEAP_PROPERTIES 数据上传_properties = f_缓存性能设置参数(D3D12_HEAP_TYPE_UPLOAD);
	hr = dev->CreateCommittedResource(
		&数据上传_properties,
		D3D12_HEAP_FLAG_NONE,
		&Buffer,
		D3D12_RESOURCE_STATE_GENERIC_READ,
		nullptr,
		IID_PPV_ARGS(m_数据上传.GetAddressOf())
	);



	//D3D12_VERTEX_BUFFER_VIEW
	
	


	return uint32();
}


void S_DX缓存::f_Fill(const void* data, uint64 size, void* CommandList) {
	D3D12_SUBRESOURCE_DATA 描述数据复制到缓冲区中 = {};
	描述数据复制到缓冲区中.pData = data;
	描述数据复制到缓冲区中.RowPitch = size;
	描述数据复制到缓冲区中.SlicePitch = 描述数据复制到缓冲区中.RowPitch;


	ID3D12GraphicsCommandList* 命令列表 = (ID3D12GraphicsCommandList*)CommandList;

	/*命令列表->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(
		m_GPU_Buffer.Get(),
		D3D12_RESOURCE_STATE_COMMON,
		D3D12_RESOURCE_STATE_COPY_DEST));*/

	//D3D12_RESOURCE_DESC 源Desc = m_数据上传->GetDesc();
	//D3D12_RESOURCE_DESC 目的Desc = m_GPU_Buffer->GetDesc();

	/*BYTE* pData;
	HRESULT hr = m_数据上传->Map(0, NULL, reinterpret_cast<void**>(&pData));
	if (FAILED(hr)) return;
	ID3D12Device* pDevice;
	m_GPU_Buffer->GetDevice(__uuidof(*pDevice), reinterpret_cast<void**>(&pDevice));*/


	/*命令列表->ResourceBarrier(1, 
		&CD3DX12_RESOURCE_BARRIER::Transition(m_GPU_Buffer.Get(),
		D3D12_RESOURCE_STATE_COMMON, 
		D3D12_RESOURCE_STATE_COPY_DEST)
	);*/

	UpdateSubresources<1>(命令列表, m_GPU_Buffer.Get(), m_数据上传.Get(), 0, 0, 1, &描述数据复制到缓冲区中);

	/*命令列表->ResourceBarrier(1, 
		&CD3DX12_RESOURCE_BARRIER::Transition(m_GPU_Buffer.Get(),
		D3D12_RESOURCE_STATE_COPY_DEST, 
		D3D12_RESOURCE_STATE_GENERIC_READ)
	);*/






}

S_GUP内存映射 S_DX缓存::f_map() {
	void* p = 0;
	D3D12_RANGE range = { 0, (m_Count*m_BitOffset) };
	//D3D12_RANGE range = { 0, 0 };

#ifdef _DEBUG
	HRESULT hr__ = 0;
#endif // _DEBUG

	DEF_DX_FunR(m_数据上传->Map(0, &range, &p));
	return { p, m_Count, m_BitOffset };
	//return { (void*)m_数据上传->GetGPUVirtualAddress(), m_Count, m_BitOffset };
}

void S_DX缓存::f_unmap() {
	//D3D12_RANGE range = { 0, (m_Count*m_BitOffset) };
	
	D3D12_RANGE range = { 0, 0 };
	m_GPU_Buffer->Unmap(0, &range);
}







